package cn.mldn.micorcloud.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import cn.mldn.vo.Dept;

@RestController
public class ConsymerDeptController {

	public static final String DEPT_GET_URL = "http://dept-8001.com:8001/dept/get/";
	public static final String DEPT_LIST_URL = "http://dept-8001.com:8001/dept/list/";
	public static final String DEPT_ADD_URL = "http://dept-8001.com:8001/dept/add";

	@Resource
	private HttpHeaders headers;

	@Resource
	private RestTemplate restTemplate;

	@RequestMapping(value = "/consumer/dept/get")
	public Object getDept(long id) {
		Dept dept = this.restTemplate
				.exchange(DEPT_GET_URL + id, HttpMethod.GET, new HttpEntity<Object>(headers), Dept.class).getBody();

		return dept;
	}

	@RequestMapping(value = "/consumer/dept/list")
	public Object listDept() {

		@SuppressWarnings("unchecked")
		List<Dept> listDepts = this.restTemplate
				.exchange(DEPT_LIST_URL, HttpMethod.GET, new HttpEntity<Object>(headers), List.class).getBody();
		return listDepts;
	}

	@RequestMapping(value = "/consumer/dept/add")
	public Object addDept(Dept dept) {
		Boolean flag = this.restTemplate
				.exchange(DEPT_ADD_URL, HttpMethod.POST, new HttpEntity<Object>(dept, headers), Boolean.class)
				.getBody();
		return flag;
	}
}
